#!/bin/bash
#********************************************************************
# Author: zhangjunjie
# Date: 2022-06-09
# FileName：centos_install_nginx.sh
#********************************************************************

### ----------- 源码安装常量的位置 ----------- ###
SRC_DIR='/usr/local/src'
PREFIX='/apps/nginx'
NGINX_VERSION="nginx-1.18.0"
NGINX_FILE="${NGINX_VERSION}.tar.gz"
NGINX_DOWN_URL="https://nginx.org/download/${NGINX_FILE}"
### ----------- 源码安装常量的位置 ----------- ###

## 先判断是否存在自定义的方法文件, 如果不存在, 则进行下载操作
if [ ! -e self_function.sh ]; then
	rpm -q wget || (yum install wget -y || { echo -e "\E[31;1m 安装 wget 软件包失败 \E[0m" ; exit; })

	echo -e "\E[33;1m 下载自定义方法中 \E[0m"
	wget https://gitee.com/gkdaxue/shell_script/raw/master/self_function.sh
	if [ ! -e self_function.sh ]; then 
		echo -e "\E[31;1m 下载自定义方法失败 \E[0m"
		exit;
	fi
fi

## 加载自定义方法
. ./self_function.sh
actions 'echo_tips' "加载自定义方法";

menus=(
"yum_epel"
"yum_nginx"
"source_code"
)
PS3="请选择你要如何安装 : "
echo "我们提供如下菜单: "
select menu in ${menus[@]} ; do
	case $menu in
		## epel 源 yum 安装
	    "yum_epel")
			## 判断是否已经安装了 nginx ,如果没有安装在进行安装操作
			rpm -q nginx &> /dev/null && (actions 'echo_error' 'nginx 已经安装了, 即将退出'; exit;) || ( actions 'echo_tips' '开始检查 yum 环境' )

			## 判断有没有 yum 源
			yum info nginx &> /dev/null 
			if [ $? -ne 0 ]; then
				wget -O self_nginx.repo https://gitee.com/gkdaxue/shell_script/raw/master/centos_yum_repolist.sh
				if [ ! -e self_nginx.repo ]; then 
					actions 'echo_error' "下载 yum 源失败, 即将退出"
					exit;
				fi

				mv self_nginx.repo /etc/yum.repos.d/
				yum clean all && yum makecache
			fi

			actions 'echo_tips' "开始 yum 安装 nginx 了"
			yum install nginx -y
			systemctl enable nginx
			systemctl start nginx
			actions 'echo_tips' "nginx 已经安装成功了, 启动信息如下"
			systemctl status nginx
			exit;
			;;
		## nginx 官网 yum 源安装, 版本比较新
		"yum_nginx")
			## 判断是否已经安装了 nginx ,如果没有安装在进行安装操作
			rpm -q nginx &> /dev/null && (actions 'echo_error' 'nginx 已经安装了, 即将退出'; exit;) || ( actions 'echo_tips' '开始检查 yum 环境' )

			## 创建 yum 源
			cat > /etc/yum.repos.d/nginx.repo <<'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF

			if [ ! -e /etc/yum.repos.d/nginx.repo ]; then 
				actions 'echo_error' "安装 nginx 官网 yum 源失败, 即将退出"
				exit;
			fi
			yum clean all && yum makecache

			actions 'echo_tips' "开始 yum 安装 nginx 了"
			yum install nginx -y
			systemctl enable nginx
			systemctl start nginx
			actions 'echo_tips' "nginx 已经安装成功了, 启动信息如下"
			systemctl status nginx
			exit;
			;;
		## 源码安装	
		"source_code")
			## 开始进行检查安装
			if [ `os_type` == "centos" -a `os_version` == '8' ] ;then
				yum install -y make vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perlExtUtils-Embed
			elif [ `os_type` == "centos" -a `os_version` == '7' ];then
				yum install -y make vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfsutils automake libxml2 libxml2-devel libxslt libxslt-devel perl perlExtUtils-Embed
			fi	
			
			actions 'echo_tips' "开始进行源码安装了"
			useradd -s /sbin/nologin -r nginx
			mkdir -p ${PREFIX}/run
			cd $SRC_DIR

			if [ ! -e $NGINX_FILE ]; then
				wget $NGINX_DOWN_URL
			fi
				
			tar xvf "${NGINX_VERSION}.tar.gz"
			cd $NGINX_VERSION
			actions 'echo_tips' "开始进行编译了"
			./configure --prefix=${PREFIX} \
			--user=nginx \
			--group=nginx \
			--with-http_ssl_module \
			--with-http_v2_module \
			--with-http_realip_module \
			--with-http_stub_status_module \
			--with-http_gzip_static_module \
			--with-pcre \
			--with-stream \
			--with-stream_ssl_module \
			--with-stream_realip_module
			make && make install
			chown -Rf nginx.nginx ${PREFIX}

			sed -i.bak -e "/#pid /c pid ${PREFIX}/run/nginx.pid;" ${PREFIX}/conf/nginx.conf
 			actions 'echo_tips' "开始创建 Service 文件"
 			echo "PATH=${PREFIX}/sbin:\$PATH" > /etc/profile.d/nginx.sh
 			cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=${PREFIX}/run/nginx.pid
ExecStartPre=/bin/rm -f ${PREFIX}/run/nginx.pid
ExecStartPre=${PREFIX}/sbin/nginx -t
ExecStart=${PREFIX}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID
LimitNOFILE=10000    
# 加上面这个,因为在 /etc/security/limits.conf 
# This file sets the resource limits for the users logged in via PAM.
# It does not affect resource limits of the system services, 所以需要我们手动加 LimitNOFILE


[Install]
WantedBy=multi-user.target
EOF
			
			## 创建日志切割功能
			cat > /etc/logrotate.d/nginx <<EOF
/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f ${PREFIX}/run/nginx.pid ]; then
                        kill -USR1 \`cat ${PREFIX}/run/nginx.pid\`
                fi
        endscript
}
EOF

			systemctl daemon-reload
			systemctl start nginx
			systemctl status nginx
			exit;
			;;			
	    *)
			echo "Selected number: $REPLY"
			echo "Selected number: $menu"
			exit;
			;;
    esac
done